<?php
// 暂时写死代码，以后考虑动态从数据库内读取
function value_to_constel($constel) {
    switch ($constel) {
        case 1:
            return '摩羯座';
        case 2:
            return '水瓶座';
        case 3:
            return '双鱼座';
        case 4:
            return '白羊座';
        case 5:
            return '金牛座';
        case 6:
            return '双子座';
        case 7:
            return '巨蟹座';
        case 8:
            return '狮子座';
        case 9:
            return '处女座';
        case 10:
            return '天秤座';
        case 11:
            return '天蝎座';
        case 12:
            return '射手座';
        default:
            return NULL;
    }    
}

function value_to_topic($post_topic) {
    switch ($post_topic) {
        case 0:
            return '自曝';
        case 1:
            return '挂牌';
        case 2:
            return '征友';
        default:
            return NULL;
    }
}

function value_to_is_single($value) {
    switch ($value) {
        case 0:
            return '是';
        case 1:
            return '否';
        case 2:
            return '保密';
        default:
            return NULL;
    }
}

function value_to_gender($value) {
    switch ($value) {
        case 0:
            return '女';
        case 1:
            return '男';
        default:
            return NULL;
    }
}
/*
 * @param string $value '2013-01-01 01:23:45'
 * @return string '<span title="2013-01-01 01:23:45">刚刚</span>'
 * '刚刚', 'x分钟前', 'x小时前’, 'x天前', 'x周前', 'x月前', 'x年前'
 */
function value_to_timestamp($value) {
    $delta = time() - strtotime($value);
    if ($delta < 60) {
        $str = '刚刚';
    } elseif ($delta < 3600) {
        $str = floor($delta / 60) . '分钟前';
    } elseif ($delta < 86400) {
        $str = floor($delta / 3600) . '小时前';
    } elseif ($delta < 604800) {
        $str = floor($delta / 86400) . '天前';
    } elseif ($delta < 2592000) {
        $str = floor($delta / 604800) . '周前';
    } elseif ($delta < 31104000) {
        $str = floor($delta / 2592000) . '月前';
    } else {
        $str = floor($delta / 31104000) . '年前';
    }
    return "<span title=\"$value\">" . $str . '</span>';
}

function post_link($post_id, $repost_id = NULL, $level_no = NULL, $pn = NULL) {
    $CI =& get_instance();
    $CI->config->load('pagination');
    
    $href = 'post/show/' . $post_id;
    $hash = ($repost_id == NULL) ? '' : ('#' . $repost_id);
    $pn = ($level_no == NULL) ? $pn : ceil($level_no / $CI->config->item('num_per_page'));
    if (($pn != NULL) && ($pn != 1)) {
        $href .= '/' . $pn . $hash;
    } else {
        $href .= $hash;
    }
    return $href;
}

function user_link($user_id, $nickname, $attr = NULL) {
    if ($attr == NULL) {
        $attr = array();
    }
    $attr['data-user-id'] = $user_id;
    $attr['target'] = '_blank';
    return anchor("home/$user_id", $nickname, $attr);
}

function extract_repost_content($content) {
    return str_replace(array('<p>', '</p>', '<br/>'), '', $content);
}

//判断出生年份是否有效的函数
function checkbirthdate($year){
    $min_age=1;
    $max_age=122;
    if(!checkdate(1,1,$year)){
        return false;
    }
    list($this_year,$this_month,$this_day)=explode('-',date('Y-m-d'));
    $min_year=$this_year-$max_age;
    $max_year=$this_year-$min_age;
    if(($year>=$min_year) && ($year<=$max_year)){
        return true;
    }else {
        return false;
    }
}

// 获取当前用户的user_id
function self_id() {
    if (isset($_SESSION['userinfo'])) {
        return $_SESSION['userinfo']['user_id'];
    } else {
        return NULL;
    }
}
?>